layui.config({
    base: '../../static/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index',//主入口模块
    treetable: 'treetable-lay/treetable'
}).use(['form']);


layui.use(['table','treetable','layer'], function(){
    var table = layui.table,
        layer = layui.layer,
        treetable = layui.treetable,
        $ = layui.$;

    var renderTable = function(){
        treetable.render({
            height: 'full-100',
            id: "dept",
            treeColIndex: 1,
            treeSpid: '0',
            treeIdName: 'deptId',
            treePidName: 'parentId',
            treeDefaultClose: false,
            treeLinkage: false,
            elem: '#dept',
            url: 'http://localhost:8888/ams-admin/dept/findAll/',
            page: false,
            cols: [ [
                {type: 'numbers'}
                , {field: 'name', width: 170, align: 'center', title: '部门名称'}
                , {field: 'createTime', width: 190, align: 'center',  title: '创建时间'}
                , {field: 'createBy', width: 100, align: 'center',  title: '创建人'}
                , {field: 'updateTime', width: 190, align: 'center',title: '修改时间'}
                , {field: 'updateBy', width: 100, align: 'center',  title: '修改人'}
                , {fixed: 'right', width: 180, align: 'center', toolbar: '#barDemo', title: '操作'}
            ] ]
        })
    };
    renderTable();

    //监听工具条
    table.on('tool(dept)', function(obj){

        var rowData = obj.data
        var dept_id = rowData.deptId

        //*************删除部门**************************
        if(obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
                $.ajax({
                    type: "POST",
                    url: "/ams-admin/dept/delete",
                    data: "deptId=" + dept_id,
                    success: function (r) {
                        if (r.code === 0) {
                            layer.close(index);
                            location.reload();
                        } else {
                            alert("删除失败" + r.msg);
                        }
                    }
                });
            });
        }

         //*************修改部门**************************
         else if(obj.event === 'edit'){
            layer.open({
                type: 2
                ,title: "修改部门" //不显示标题栏
                ,closeBtn: 2
                ,area: ['700px', '500px']
                ,shade: 0.8
                // ,btn: ['确定', '取消']
                ,btnAlign: 'c'
                ,content: 'update_dept.html'
                ,success: function(layero,frame,frame2){
                    var iframein = window[layero.find('iframe')[0]['name']];
                    iframein.$("#name").val(rowData.name);
                    iframein.$("#dept_id").val(rowData.deptId);
                    iframein.$("#parent_id").val(rowData.parentId);
                    iframein.$("#parentName").val(rowData.parentName);
                }
            })
        }
    });

    //*************新增部门**************************
    let active = {

        add: function () { //新增部门
            layer.open({
                type: 2
                , title: "新增部门" //不显示标题栏
                , closeBtn: 1
                , area: ['700px', '500px']
                , btnAlign: 'c'
                , content: 'addDept.html'
                , success: function (layero) {

                }
            });
        }
    };

    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});
